
package com.sskj.jkzx.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.EmpQueryParam;
import com.sskj.jkzx.domain.entity.emp.EmpBaseInfo;
import com.sskj.jkzx.domain.entity.emp.EmpEngInfo;
import com.sskj.jkzx.domain.entity.emp.EmpZsInfo;
import com.sskj.jkzx.domain.entity.eng.FileInfo;
import com.sskj.jkzx.vo.TimeLineBean;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "**/sskj/emp")
@Api(value = "个人信息", description = "个人信息")
public class EmpInfoServiceImpl {

	protected final Log logger = LogFactory.getLog(EmpInfoServiceImpl.class);

	@Value("${sskj.cmisServer}")
	private String cmisServer;

	@Value("${sskj.jdServer}")
	private String jdServer;

	/***
	 * 个人基本信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/jbxx", method = RequestMethod.GET)
	@ApiOperation(value = "个人基本信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "empId", value = "ID", paramType = "form", dataType = "string") })
	public EmpBaseInfo queryEmpBaseInfo(@ApiIgnore EmpQueryParam empQueryParam) {

		EmpBaseInfo result = new EmpBaseInfo();
		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/emp/jbxx/" + empQueryParam.getEmpId(),
					null);

			result = JSONObject.parseObject(ss, EmpBaseInfo.class);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONObject.toJSONString(result));

		return result;
	}

	/***
	 * 时间轴信息查询
	 * 
	 * @param empQueryParam
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/timeline", method = RequestMethod.GET)
	@ApiOperation(value = "时间轴信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "orgId", value = "企业ID", paramType = "form", dataType = "string") })
	public List<TimeLineBean> queryOrgTimelineInfo(@ApiIgnore EmpQueryParam empQueryParam) {

		List<TimeLineBean> results = new ArrayList<TimeLineBean>();

		String str;
		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxlr/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/erpxxzz/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/erpxxlz/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxba/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxBgIn/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxBgOut/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxztb/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxztbjs/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxht/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxsgxk/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/timeLine/emp/empxxjy/" + empQueryParam.getEmpId(),
					null);

			if (str != null) {
				results.addAll(JSON.parseArray(str, TimeLineBean.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		sort(results, "time", true);

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 照片信息
	 * 
	 * @return
	 * @throws IOException
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zpxx", method = RequestMethod.GET)
	@ApiOperation(value = "照片信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "ID", paramType = "query", dataType = "string") })
	public void queryZbtzsPDF(String docId, HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		FileInfo result = new FileInfo();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/file/jkzx/emp/zpxx/" + docId, null);

			if (ss != null) {
				result = JSONObject.parseObject(ss, FileInfo.class);
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		if (result.getData() != null) {
			response.addHeader("content-type", "image/JPEG");
			response.addHeader("content-type", "image/GIF");
			response.addHeader("content-type", "image/PNG");
			response.getOutputStream().write(result.getData());
		}
	}

	/***
	 * 个人证书信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zsxx", method = RequestMethod.GET)
	@ApiOperation(value = "个人证书信息", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "empId", value = "ID", paramType = "form", dataType = "string") })
	public List<EmpZsInfo> queryEmpZsInfo(@ApiIgnore EmpQueryParam empQueryParam) {

		List<EmpZsInfo> results = new ArrayList<EmpZsInfo>();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/emp/zsxx/" + empQueryParam.getEmpId(),
					null);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, EmpZsInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 在建项目信息查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zjxmxx", method = RequestMethod.GET)
	@ApiOperation(value = "在建项目信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "empId", value = "ID", paramType = "form", dataType = "string") })
	public List<EmpEngInfo> queryZEmpEngInfo(@ApiIgnore EmpQueryParam empQueryParam) {

		List<EmpEngInfo> results = new ArrayList<EmpEngInfo>();

		try {
			String ss = HttpHelp.sendGet(jdServer + "/seam/resource/v1/jkzx/emp/zjxmxx/" + empQueryParam.getEmpId(),
					null);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, EmpEngInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 业绩信息查询
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/yjxmxx", method = RequestMethod.GET)
	@ApiOperation(value = "业绩信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "empId", value = "ID", paramType = "form", dataType = "string") })
	public List<EmpEngInfo> queryYEmpEngInfo(@ApiIgnore EmpQueryParam empQueryParam) {

		List<EmpEngInfo> results = new ArrayList<EmpEngInfo>();

		try {
			String ss = HttpHelp.sendGet(jdServer + "/seam/resource/v1/jkzx/emp/yjxmxx/" + empQueryParam.getEmpId(),
					null);

			if (ss != null) {
				results.addAll(JSON.parseArray(ss, EmpEngInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/**
	 * @describe 依据某个字段对集合进行排序
	 * @author ...
	 * @date 2013-1-22 下午3:44:47
	 * @param list
	 *            待排序的集合
	 * @param fieldName
	 *            依据这个字段进行排序
	 * @param asc
	 *            如果为true，是正序；为false，为倒序
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public <T> void sort(List<T> list, String fieldName, boolean asc) {

		Comparator<?> mycmp = ComparableComparator.getInstance();
		mycmp = ComparatorUtils.nullLowComparator(mycmp); // 允许null
		if (!asc) {
			mycmp = ComparatorUtils.reversedComparator(mycmp); // 逆序
		}
		Collections.sort(list, new BeanComparator(fieldName, mycmp));
	}

}
